package org.micro.rbac.core.result;

import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author hongsir
 * @apiNote 空
 * @since 2021-03-02 04:11:29
 */
@Data
@NoArgsConstructor
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 7936286089104301875L;

    @JSONField(ordinal = 1)
    private String code;
    @JSONField(ordinal = 2)
    private String msg;
    @JSONField(ordinal = 3)
    private T data;

    public R(T data, String code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static <T> R<T> ok(String msg) {
        return of(null, Code.SUCCESS.code(), msg);
    }

    public static <T> R<T> ok(T model, String msg) {
        return of(model, Code.SUCCESS.code(), msg);
    }

    public static <T> R<T> ok(T model) {
        return of(model, Code.SUCCESS.code(), Code.SUCCESS.message());
    }

    public static <T> R<T> ok() {
        return of(null, Code.SUCCESS.code(), Code.SUCCESS.message());
    }

    public static <T> R<T> of(T data, String code, String msg) {
        return new R<>(data, code, msg);
    }

    public static <T> R<T> of(ICode code) {
        return of(code, code.getMessage());
    }

    public static <T> R<T> of(ICode code, String msg) {
        return of(null, code.getCode(), msg);
    }


    public static <T> R<T> fail(String msg) {
        return of(null, Code.FAIL.code(), msg);
    }

    public static <T> R<T> fail(T model, String msg) {
        return of(model, Code.FAIL.code(), msg);
    }

    public static <T> R<T> fail() {
        return of(null, Code.FAIL.code(), Code.FAIL.message());
    }

    public static <T> R<T> judge(boolean status) {
        return status ? ok() : fail();
    }

}
